package com.reguang.system.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.reguang.pojo.SysDeptDO;
import com.reguang.pojo.SysMenuDO;
import com.reguang.result.Result;
import com.reguang.service.SysDeptService;
import com.reguang.utils.Tree;

/**
 * 
 * <p>
 * Title: Dept
 * </p>
 * <p>
 * Description: 部门管理
 * </p>
 * 
 * @author weibiao
 * @date 2020-01-16 09:17:26
 */
@Controller
@RequestMapping("/system/sysDept")
public class SysDeptController {

	private String prefix = "system/dept";

	@Autowired
	private SysDeptService deptService;

	@GetMapping()
	String Dept() {
		return prefix + "/dept";
	}

	@GetMapping("/list")
	@ResponseBody
	List<SysDeptDO> list(@RequestParam Map<String, Object> params) {
		// 返回列表数据
		return deptService.list(params);
	}

	@GetMapping("/add/{pId}")
	String add(@PathVariable("pId") Integer pId, Model model) {
		model.addAttribute("pId", pId);
		if (pId == 0) {
			model.addAttribute("pName", "总部门");
		} else {
			SysDeptDO deptDo = new SysDeptDO();
			deptDo.setId(pId);
			model.addAttribute("pName", deptService.queryOne(deptDo).getName());

		}
		return prefix + "/add";
	}

	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Integer id, Model model) {
		SysDeptDO dept = deptService.queryByPrimaryKey(id);
		model.addAttribute("dept", dept);
		return prefix + "/edit";
	}

	@ResponseBody
	@PostMapping("/save")
	public Result save(SysDeptDO dept) {
		if (deptService.save(dept) > 0) {
			return Result.success();
		}
		return Result.failure();
	}

	@ResponseBody
	@RequestMapping("/update")
	public Result update(SysDeptDO dept) {
		if (deptService.update(dept) > 0) {
			return Result.success();
		}
		return Result.failure();
	}

	@PostMapping("/remove")
	@ResponseBody
	public Result remove( Integer id) {
		if (deptService.deleteById(id) > 0) {
			return Result.success();
		}
		return Result.failure();
	}

	@GetMapping("/tree")
	@ResponseBody
	public Tree<SysDeptDO> tree() {
		Tree<SysDeptDO> tree = new Tree<SysDeptDO>();
		tree = deptService.getTree();
		return tree;
	}

	@GetMapping("/treeView")
	String treeView() {
		return prefix + "/deptTree";
	}
}
